#!/usr/bin/env python
import time
import Seven_Segment_Display as display

display.init()

state = False
prev_state = False
mode = 0
click_time = 0

while True:
  state = display.get_switch()
  # GPIO Input rising slope
  if state and not prev_state:
    mode = (mode+1)%3
    click_time = time.time()
  prev_state = state
 
  # there is no switch-case statement in Python
  if mode == 0:
    display.set_double_digit(0,time.localtime().tm_min)
    display.set_double_digit(1,time.localtime().tm_hour)
  elif mode == 1:
    display.set_double_digit(0,time.localtime().tm_sec)
    # sets digits 2 and 3 to blank
    display.set_digit(2,10)
    display.set_digit(3,10)
  else:
    display.set_double_digit(0,time.localtime().tm_mon)
    display.set_double_digit(1,time.localtime().tm_mday)
  
  # display time after 5 seconds
  if mode != 0 and time.time() > click_time + 5:
    mode = 0
  
  # perform display test every hour
  if time.localtime().tm_sec == 0 and time.localtime().tm_min == 0:
    display.test()
  
  # reduce cpu load
  time.sleep(0.05)
